﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClientDBAgent;


namespace HF.Business.Common
{
    /// <summary>
    /// AuditMessage 的摘要说明
    /// </summary>
    public class AuditMessage
    {
        public AuditMessage()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 审批信息
        /// </summary>
        public string AuditId;
        public string WorkflowId = "";
        public string WorkflowInsId = "";
        public string WorktaskId = "";
        public string WorktaskInsId = "";
        public string OperatorInsId = "";
        public string Message = "";
        public string AuditUserId = "";
        public string AuditUserName = "";
        public string AuditDuty = "";
        public string AuditArch = "";
        public string AuditResult = "";

        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList ={ "AuditId", "WorkflowId", "WorkflowInsId", "WorktaskId", "WorktaskInsId",
                                "OperatorInsId","Message","AuditUserId","AuditUserName","AuditDuty","AuditArch",
                                "AuditResult"};
        private string tableName = "Comm_AuditMessage";
        private string keyField = "AuditId";
        private string sqlString = "";

        /// <summary>
        /// 获得流程实例的审批列表
        /// </summary>
        /// <param name="FatherClassId">流程实例Id</param>
        /// <returns>审批列表table</returns>
        public static DataTable GetAuditMsgTableByFlow(string workflowInsId)
        {
            try
            {
                string tmpStr = "select * from Comm_AuditMessage where  workflowInsId=@workflowInsId order by AuditTime";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workflowInsId", workflowInsId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得操作实例审批列表
        /// </summary>
        /// <param name="FatherClassId">操作实例Id</param>
        /// <returns>审批列表table</returns>
        public static DataTable GetAuditMsgTableByOper(string operatorInsId)
        {
            try
            {
                string tmpStr = "select * from Comm_AuditMessage where  OperatorInsId=@operatorInsId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@operatorInsId", operatorInsId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void setParameter()
        {
           sqlDataItem.ParameterList.Clear();;
            sqlDataItem.AppendParameter("@AuditId", AuditId);
            sqlDataItem.AppendParameter("@WorkflowId", WorkflowId);
            sqlDataItem.AppendParameter("@WorkflowInsId", WorkflowInsId);
            sqlDataItem.AppendParameter("@WorktaskId", WorktaskId);
            sqlDataItem.AppendParameter("@WorktaskInsId", WorktaskInsId);
            sqlDataItem.AppendParameter("@OperatorInsId", OperatorInsId);
            sqlDataItem.AppendParameter("@Message", Message);
            sqlDataItem.AppendParameter("@AuditUserId", AuditUserId);
            sqlDataItem.AppendParameter("@AuditUserName", AuditUserName);
            sqlDataItem.AppendParameter("@AuditDuty", AuditDuty);
            sqlDataItem.AppendParameter("@AuditArch", AuditArch);
            sqlDataItem.AppendParameter("@AuditResult", AuditResult);

        }
        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ",";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";

            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }
        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = this.fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + "=@" + tmpFieldName + ",";
            }
            tmpFieldName = fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + "=@" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "='" + this.AuditId + "'";
            sqlDataItem.CommandText = sqlString;
        }

        /// <summary>
        /// 新建 审批信息
        /// </summary>
        public void InsertAuditMessage()
        {
            if (AuditId.Trim().Length == 0 || AuditId == null)
                throw new Exception("InsertAuditMessage方法错误，AuditId 不能为空！");

            try
            {
                setInsertSql();//设定insert语句
                setParameter();//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 修改一个审批信息
        /// </summary>
        public void UpdateAuditMessage()
        {
            if (AuditId.Trim().Length == 0 || AuditId == null)
                throw new Exception("UpdateAuditMessage方法错误，AuditId 不能为空！");

            try
            {
                setUpdateSql();//设定定Update语句
                setParameter();//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
